package com.els.search.service.imlp;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.els.search.entity.FakeNews;
import com.els.search.mapper.FakeNewsMapper;
import com.els.search.service.FakeNewsService;
import com.els.search.utils.ESconst;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.directory.SearchResult;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @title: FakeNewsServiceImpl
 * @projectName fake-baidu
 * @Date: 2021/7/12 12:43
 * @Author: lei.yu-esint
 * @Description:
 */
@Service
public class FakeNewsServiceImpl implements FakeNewsService {

    @Autowired(required = false)
    private FakeNewsMapper fakeNewsMapper;

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Override
    public IPage<FakeNews> selectData(String keyword,String page,String size) {

        //初始化页码
        Long pageN=Long.valueOf(page);
        Long sizeN=Long.valueOf(size);

        //分页构造
        IPage<FakeNews> pageParam = new Page<>(pageN, sizeN);

        //条件构造
        Wrapper wrapper=new QueryWrapper();
        ((QueryWrapper) wrapper).like(ESconst.ES_INDEX_NEWS,keyword);

        IPage<FakeNews> listData = fakeNewsMapper.selectPage(pageParam, wrapper);
        return listData;
    }

    @Override
    public String selectDataEs(String keyword,String page,String size) {

        //初始化页码
        Long pageN=Long.valueOf(page);
        Long sizeN=Long.valueOf(size);

        //关键字
        SearchRequest searchResult = new SearchRequest(ESconst.ES_INDEX_NEWS);

        //构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //查询条件使用QueryBuilders来构建
        TermQueryBuilder termQueryBuilder=QueryBuilders.termQuery("news_title", keyword);//精确查询
        MatchAllQueryBuilder matchAllQueryBuilder=QueryBuilders.matchAllQuery();//匹配所有
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.query(matchAllQueryBuilder);

        //传入查询条件
        searchResult.source(searchSourceBuilder);

        try {
            SearchResponse searchResponse=restHighLevelClient.search(searchResult,RequestOptions.DEFAULT);

            for(SearchHit docfiles:searchResponse.getHits().getHits()){

                System.out.println(docfiles.getSourceAsMap());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public FakeNews addData() {
        Date date=new Date();
        FakeNews fakeNews=new FakeNews();
//        fakeNews.setNewsId(UUID.randomUUID().toString());
        fakeNews.setNewsTitle("海绵宝宝玩偶!!");
        fakeNewsMapper.insert(fakeNews);
        return fakeNews;
    }
}
